package org.tuoren.official.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tuoren.common.core.domain.R;
import org.tuoren.common.core.validate.AddGroup;
import org.tuoren.common.core.validate.EditGroup;
import org.tuoren.common.excel.utils.ExcelUtil;
import org.tuoren.common.idempotent.annotation.RepeatSubmit;
import org.tuoren.common.log.annotation.Log;
import org.tuoren.common.log.annotation.VisitLog;
import org.tuoren.common.log.enums.BusinessType;
import org.tuoren.common.mybatis.core.page.PageQuery;
import org.tuoren.common.mybatis.core.page.TableDataInfo;
import org.tuoren.common.web.core.BaseController;
import org.tuoren.official.domain.bo.TQrCodeBo;
import org.tuoren.official.domain.vo.TQrCodeVo;
import org.tuoren.official.service.ITQrCodeService;

import java.util.List;

/**
 * 二维码信息维护
 *
 * @author Lion Li
 * @since 2024-11-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/official/qrCode")
public class TQrCodeController extends BaseController {

    private final ITQrCodeService tQrCodeService;

    /**
     * 查询二维码信息维护列表
     */
    @SaCheckPermission("official:qrCode:list")
    @GetMapping("/list")
    public TableDataInfo<TQrCodeVo> list(TQrCodeBo bo, PageQuery pageQuery) {
        return tQrCodeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出二维码信息维护列表
     */
    @SaCheckPermission("official:qrCode:export")
    @Log(title = "二维码信息维护", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TQrCodeBo bo, HttpServletResponse response) {
        List<TQrCodeVo> list = tQrCodeService.queryList(bo);
        ExcelUtil.exportExcel(list, "二维码信息维护", TQrCodeVo.class, response);
    }

    /**
     * 获取二维码信息维护详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("official:qrCode:query")
    @GetMapping("/{id}")
    public R<TQrCodeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tQrCodeService.queryById(id));
    }

    /**
     * 新增二维码信息维护
     */
    @SaCheckPermission("official:qrCode:add")
    @Log(title = "二维码信息维护", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TQrCodeBo bo) {
        return toAjax(tQrCodeService.insertByBo(bo));
    }

    /**
     * 修改二维码信息维护
     */
    @SaCheckPermission("official:qrCode:edit")
    @Log(title = "二维码信息维护", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TQrCodeBo bo) {
        return toAjax(tQrCodeService.updateByBo(bo));
    }

    /**
     * 删除二维码信息维护
     *
     * @param ids 主键串
     */
    @SaCheckPermission("official:qrCode:remove")
    @Log(title = "二维码信息维护", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tQrCodeService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 官网-获取二维码信息列表（页面底部：0；合作通道：传入相应菜单Id）     *
     * @param menuId 菜单Id
     */
    @SaIgnore
    @VisitLog()
    @GetMapping("/getlist")
    public TableDataInfo<TQrCodeVo> list(Long menuId,String plat) {
        return tQrCodeService.queryList(menuId,plat);
    }
}
